import {createRouter, createWebHistory} from "vue-router";
import Home from "../views/Home.vue";
import axios from "axios";

const routes = [
    {
        path: '/',
        redirect: '/login'
    }, {
        path: "/",
        name: "Home",
        component: Home,
        children: [
            {
                path: "/dashboard",
                name: "dashboard",
                meta: {
                    title: '系统首页'
                },
                component: () => import (
                /* webpackChunkName: "dashboard" */
                "../views/Dashboard.vue")
            },{
                path: "aliyun_ecs_list",
                name: "aliyun_ecs_list",
                meta: {
                    title: 'ecs列表'
                },
                component: () => import (
                    "../views/aliyun/ecs/list.vue")
            }
            ,{
                path: "/docker_container",
                name: "docker_container",
                meta: {
                    title: 'Docker容器'
                },
                component: () => import (
                    "../views/docker/container.vue")
            }, {
                path: "/docker_images",
                name: "docker_images",
                meta: {
                    title: 'Docker镜像'
                },
                component: () => import (
                    "../views/docker/images.vue")
            }, {
                path: "/table",
                name: "basetable",
                meta: {
                    title: '表格'
                },
                component: () => import (
                /* webpackChunkName: "table" */
                "../views/BaseTable.vue")
            }, {
                path: "/charts",
                name: "basecharts",
                meta: {
                    title: '图表'
                },
                component: () => import (
                /* webpackChunkName: "charts" */
                "../views/BaseCharts.vue")
            }, {
                path: "/form",
                name: "baseform",
                meta: {
                    title: '表单'
                },
                component: () => import (
                /* webpackChunkName: "form" */
                "../views/BaseForm.vue")
            }, {
                path: "/tabs",
                name: "tabs",
                meta: {
                    title: 'tab标签'
                },
                component: () => import (
                /* webpackChunkName: "tabs" */
                "../views/Tabs.vue")
            }, {
                path: "/donate",
                name: "donate",
                meta: {
                    title: '鼓励作者'
                },
                component: () => import (
                /* webpackChunkName: "donate" */
                "../views/Donate.vue")
            }, {
                path: "/permission",
                name: "permission",
                meta: {
                    title: '权限管理',
                    permission: true
                },
                component: () => import (
                /* webpackChunkName: "permission" */
                "../views/Permission.vue")
            }, {
                path: "/i18n",
                name: "i18n",
                meta: {
                    title: '国际化语言'
                },
                component: () => import (
                /* webpackChunkName: "i18n" */
                "../views/I18n.vue")
            }, {
                path: "/upload",
                name: "upload",
                meta: {
                    title: '上传插件'
                },
                component: () => import (
                /* webpackChunkName: "upload" */
                "../views/Upload.vue")
            }, {
                path: "/icon",
                name: "icon",
                meta: {
                    title: '自定义图标'
                },
                component: () => import (
                /* webpackChunkName: "icon" */
                "../views/Icon.vue")
            }, {
                path: '/404',
                name: '404',
                meta: {
                    title: '找不到页面'
                },
                component: () => import (/* webpackChunkName: "404" */
                '../views/404.vue')
            }, {
                path: '/403',
                name: '403',
                meta: {
                    title: '没有权限'
                },
                component: () => import (/* webpackChunkName: "403" */
                '../views/403.vue')
            }
        ]
    }, {
        path: "/login",
        name: "Login",
        meta: {
            title: '登录'
        },
        component: () => import (
        /* webpackChunkName: "login" */
        "../views/Login.vue")
    },
    {
        path: "aliyun_ecs_list",
        name: "aliyun_ecs_list",
        meta: {
            title: 'ecs列表'
        },
        component: () => import (
            "../views/aliyun/ecs/list.vue")
    }
];

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
});

let ISLOGIN = false
async function isLogin(){
    await  axios.get('/isLogin')
        .then((response)=>{
            if (response.data.code == 0){
                ISLOGIN = true
            }
        })
        .catch(function (error) {
            console.log(error);
        });
     return ISLOGIN
}

router.beforeEach((to, from, next) => {
    // document.title = `${to.meta.title} | vue-manage-system`;
    // if (to.path !== '/login') {
    //      next('/login');
    //     axios.get('/isLogin')
    //         .then((response)=>{
    //             if(response.data.code == 1001){
    //                 console.log("11")
    //                 return next('/login');
    //             }
    //         })
    //         .catch(function (error) {
    //             console.log(error);
    //         });
    // }

    // document.title = `${to.meta.title} | vue-manage-system`;
    // if (to.path !== '/login') {
    //     ISLOGIN = isLogin()
    //     if(!ISLOGIN){
    //         next('/login')
    //     }else {
    //         next()
    //     }
    // }
    // // } else if (to.meta.permission) {
    // //     // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
    // //     role === 'admin'
    // //         ? next()
    // //         : next('/403');
    // // }
    // else {
    //     next();
    // }
    console.log(to.path)
    ISLOGIN = isLogin()
    next()
});

export default router;